namespace ghost {
	export class DirectorBoard extends GYSprite{
		public back:Laya.Sprite;
		public dir:Laya.Sprite;
		public zhou:Laya.Sprite;
		private _player:map2D.IRole;
		private _touchId:number;
		private _dirInitX:number;
		private _dirInitY:number;				
		public running:boolean;
		public percent:number;
		private _radius:number;
		public constructor() {
			super();
			let s = this;								
			s._dirInitX = 300;
			s._dirInitY = 300;
			s.width = 600;
			s.height = 700;
			s.back = new Laya.Sprite;
			s.back.texture = Main.instance.getRes(Conf.dirBoard + "dipan.png");
			s.addChild(s.back);
			s.dir = new Laya.Sprite;
			s.dir.texture = Main.instance.getRes(Conf.dirBoard + "fx.png");			
			s.zhou = new Laya.Sprite;
			s.zhou.texture = Main.instance.getRes(Conf.dirBoard + "zhou.png");
			s._radius = s.back.texture.width >> 1;
			s.zhou.pivotX = s.zhou.pivotY = s.zhou.texture.width >> 1;			
			s.dir.pivotX = s.dir.pivotY = s.dir.texture.width >> 1;
			s.back.pivotX = s.back.pivotY = s._radius;
						
			s.mouseEnabled = true;
			s.mouseThrough = false;
			s.on(Laya.Event.MOUSE_DOWN, s, s.touchBegin);
			s.on(Laya.Event.MOUSE_UP, s, s.touchEnd);
			document.addEventListener("mouseup",s.touchEnd);
			// Laya.stage.on(Laya.Event.MOUSE_UP, s, s.touchEnd);
			s._touchId = NaN;
			s.setPos(s._dirInitX,s._dirInitY);			
		}
		private touchMove(t:number):void
		{
			let s = this;
			let toX:number,toY:number;
			let cX:number,cY:number;			
			let disX:number,disY:number;
			let agl:number,r:number;
			
			if(s._touchId!=s._touchId)return;
			r = s._radius;						
			toX = s.mouseX;
			toY = s.mouseY;
			cX = s.back.x;
			cY = s.back.y;
			if(cY == toY)return;			
			disY = toY - cY;
			disX = toX - cX;
			agl = Math.atan2(disY, disX);
			s.touchDir(agl, Math.pow(disY*disY+disX*disX,0.5));
		}
		public touchDir(agl:number, touchR:number):void
		{
			if(GameManager.getInstance().isGameOver)return;		
			let s= this;
			let r:number;
			let cX:number,cY:number;			
			r = s._radius;			
			cX = s.back.x;
			cY = s.back.y;
			r = r*0.44|0;
			if(touchR > r)
			{				
				s.zhou.x = cX + Math.cos(agl) * r;
				s.zhou.y = cY +  Math.sin(agl) * r;
			}
			else
			{
				s.zhou.x = cX + Math.cos(agl) * touchR;
				s.zhou.y = cY +  Math.sin(agl) * touchR;
			}
			s.dir.rotation = agl / Math.PI * 180;
			let direct:number;
			if(s._player)
			{
				direct = map2D.DirectionType.getDirectorSts(agl,map2D.DirectionMode.EIGHT);
				s._player.setDirection(direct);
			}	
		}
		public touchBegin(e:Laya.Event=null):void
		{
			let s= this;				
			if(GameManager.getInstance().isGameOver)return;					
			if(e)
			{
				// console.log("start",e.touchPointID,s._touchId);
				if(s._touchId == s._touchId && e.touchId != s._touchId)return;
				s._touchId = e.touchId;
				CommonUtil.addStageLoop(s.touchMove, s);
			}				
			s.addChild(s.zhou);					
			s.addChild(s.dir);				
			s.touchMove(Date.now());
			s.setPos(s.mouseX, s.mouseY)			
		}
		private setPos(x:number=0,y:number=200):void
		{
			let s= this;
			s.back.y = y;
			s.back.x = x;
			s.dir.x = x;
			s.dir.y = y;
		}
		public touchEnd=(e:any=null):void=>
		{
			let s= this;			
			if(s.zhou.parent == null)return;		
				
			if(e && e.touchId != null)
			{
				// console.log("end",e.touchPointID,s._touchId);
				if(e.touchId != s._touchId)return;				
			}
			CommonUtil.delStageLoop(s.touchMove, s);
			s._touchId = NaN;
			s._player.setDirection(-1);			
			s.setPos(s._dirInitX,s._dirInitY);	
			s.removeChild(s.zhou);					
			s.removeChild(s.dir);						
		}		
		public setData(player:map2D.IRole):void
		{
			let s= this;
			s._player = player;
		}
		public destroy(destroyChild:boolean=true):void
		{
			super.destroy(destroyChild);
			let s= this;
			document.removeEventListener("mouseup",s.touchEnd);
			// Laya.stage.off(Laya.Event.MOUSE_UP, s, s.touchEnd);				
		}
	}
}